<template>
  <basic-container>
    <div v-show="showSearchFlag" class="flex-start m-t-20 m-b-10">
      <div class="flex-center">
        <el-input v-model="search.groupName" class="m-r-10" placeholder="请输入工厂名称" size="small" @keyup.native.enter="query"
                  style="width: 300px;">
          <el-button slot="append" :loading="loading" icon="el-icon-search" size="small" type="primary"  @click="query">
            查询
          </el-button>
        </el-input>
      </div>
    </div>
    <div class="flex-center m-b-10">
      <div></div>
      <div class="flex-center">
        <el-button :loading="loading" circle icon="el-icon-refresh" @click="query"></el-button>
        <el-button circle icon="el-icon-search" @click="showSearchFlag=!showSearchFlag"></el-button>
<!--        <el-button size="small" type="danger" @click="remove">删除</el-button>-->
        <el-button size="small" type="primary" @click="showAddFlag=true">新增</el-button>
        <el-button size="small"  @click="reset">重置</el-button>
      </div>
    </div>
    <el-table v-loading="loading" :data="data" :header-cell-style="{background:'#eee',color:'#000'}" border
              class="my-table"
              style="width: 100%">
<!--      <el-table-column align="center" type="selection" width="60"/>-->
      <el-table-column align="center" label="序号" type="index" width="60"/>
      <el-table-column label="名称" prop="price">
        <template v-slot="scope">{{ scope.row.groupName | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="编号">
        <template v-slot="scope">{{ scope.row.groupKey | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="地址">
        <template v-slot="scope">{{ scope.row.groupKey | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template v-slot="scope">{{ scope.row.groupKey | defaultValue }}</template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template v-slot="scope">
          <el-button icon="el-icon-remove" size="small" type="text" @click="toRemove(scope.row)">删除</el-button>
          <el-button icon="el-icon-remove" size="small" type="text" @click="toEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <app-pagination ref="paginationRef" :page-size="page.size" :total="total" class="m-t-20" @change="pageChange"/>
    <div class="h-100px"></div>

    <el-dialog :visible.sync="showAddFlag" append-to-body title="新增产品" width="500px">
      <el-form label-width="120px">
        <el-form-item label="名称" required>
          <search-product width="200" title="" @select="form.productId = $event.ids"></search-product>
        </el-form-item>
        <el-form-item label="编号" required>
          <el-input style="width: 200px" v-model="form.groupName" size="small"/>
        </el-form-item>
        <el-form-item label="地址" required>
          <el-input style="width: 200px" v-model="form.groupName" size="small"/>
        </el-form-item>
      </el-form>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button size="small" @click="closeAddAndEdit">取消</el-button>
        <el-button :loading="submitLoading" size="small" type="primary" @click="doConfirm">确定</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import AppPagination from "@/components/business/app-pagination";
import {
  getRecords,
  getTotal,
  isStringEmpty,
  msgError,
  confirmWarning, msgSuccess
} from "@/util/common-util";
import AuthWrapper from "@/components/business/auth-wrapper";
import request from "@/router/axios";
import {deleteRecord} from "@/api/business/filling-team";
import SearchProduct from "@/components/SearchCondition/SearchProduct";
import SearchProductGroup from "@/components/SearchCondition/SearchProductGroup";
import {getIotGroupList, getIotGroupPage, iotGroupRemove, iotGroupSubmit} from "@/api/product/deviceGroup";
import AppFileUpload from "@/components/business/app-file-upload";


export default {
  name: "FactoryList",
  components: {AppFileUpload, SearchProductGroup, SearchProduct, AuthWrapper, AppPagination},
  data() {
    return {
      page: {
        size: 20,
        current: 1
      },
      total: 0,
      searchDate: '',
      search: {
        groupName: '',
      },
      form: {
        groupName: '',
        groupKey: '',
      },
      showSearchFlag: true,
      loading: false,
      data: [{}],
      showAddFlag: false,
      submitLoading: false,
      isEdit: false,
      options: [{
        value: 'zhinan',
        label: '指南',
        children: [{
          value: 'shejiyuanze',
          label: '设计原则',
          children: [{
            value: 'yizhi',
            label: '一致'
          }, {
            value: 'fankui',
            label: '反馈'
          }, {
            value: 'xiaolv',
            label: '效率'
          }, {
            value: 'kekong',
            label: '可控'
          }]
        }, {
          value: 'daohang',
          label: '导航',
          children: [{
            value: 'cexiangdaohang',
            label: '侧向导航'
          }, {
            value: 'dingbudaohang',
            label: '顶部导航'
          }]
        }]
      }]
    }
  },
  mounted() {
    this.query()
  },
  methods: {
    genCode() {

    },
    toEdit(row) {
      this.form = {...row}
    },
    pageChange(_page) {
      this.page = _page
      this.query()
    },
    refresh() {
      this.$refs.paginationRef.reset()
    },
    query() {
      this.loading = true
      getIotGroupPage({...this.page, ...this.search}).then(res => {
        this.data = getRecords(res)
        this.total = getTotal(res)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    addNew() {
      this.showAddFlag = true
    },
    doConfirm() {
      if (isStringEmpty(this.form.groupName)) return msgError("请输入分组名称")
      if (isStringEmpty(this.form.groupKey)) return msgError("请输入groupKey")
      this.submitLoading = true
      iotGroupSubmit(this.form).then(res => {
        this.submitLoading = false
        this.page.current = 1
        this.query()
        this.closeAddAndEdit()
      }).finally(() => {
        this.submitLoading = false
      })
    },
    toDetail(row) {
     this.$emit('detail', row)
    },
    closeAddAndEdit() {
      this.showAddFlag = false
      this.form.productId = ''
      this.form.groupKey = ''
      this.form.groupName = ''
    },
    toRemove(row) {
      confirmWarning('删除操作', `确定删除设备分组 ${row.groupName} ？`, () => {
        iotGroupRemove({ids: row.id}).then(res => {
          this.query()
          msgSuccess()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.my-table {
  /deep/ .el-button--small {
    padding: 0 5px !important;
    font-size: 14px !important;
  }

  /deep/ td {
    padding: 8px 0 !important;
  }
}
</style>
